/*
 * misc.h
 *
 *  Created on: Jun 29; 2012
 *      Author: Tiberiu S. Petre
 */

#include <stdint.h>
#include <stdio.h>
#include "rpe_ltp_rx.h"
#include "rpe_ltp_tx.h"

#ifndef MISC_H_
#define MISC_H_

typedef struct {
	// For each primary frame
	int16_t* LARc;
	// For each secondary frame
	int16_t* LTP_P1;
	int16_t* LTP_b1;
	int16_t* RPE_M1;
	int16_t* RPE_xmaxc1;
	int16_t* RPE_xMc1;

	int16_t* LTP_P2;
	int16_t* LTP_b2;
	int16_t* RPE_M2;
	int16_t* RPE_xmaxc2;
	int16_t* RPE_xMc2;

	int16_t* LTP_P3;
	int16_t* LTP_b3;
	int16_t* RPE_M3;
	int16_t* RPE_xmaxc3;
	int16_t* RPE_xMc3;

	int16_t* LTP_P4;
	int16_t* LTP_b4;
	int16_t* RPE_M4;
	int16_t* RPE_xmaxc4;
	int16_t* RPE_xMc4;
} unpacked_hf;

typedef int8_t packed_hf[33];

typedef int16_t pcm_frame_t[160];


#define unpack_decoder_hf(phf, uphf) \
	unpack_decoder_homing_frame(\
			uphf.LARc,\
			uphf.LTP_P1, uphf.LTP_b1, uphf.RPE_M1, uphf.RPE_xmaxc1,\
			uphf.RPE_xMc1,\
			uphf.LTP_P2, uphf.LTP_b2, uphf.RPE_M2, uphf.RPE_xmaxc2,\
			uphf.RPE_xMc2,\
			uphf.LTP_P3, uphf.LTP_b3, uphf.RPE_M3, uphf.RPE_xmaxc3,\
			uphf.RPE_xMc3,\
			uphf.LTP_P4, uphf.LTP_b4, uphf.RPE_M4, uphf.RPE_xmaxc4,\
			uphf.RPE_xMc4,\
			phf\
	);


#define pack_decoder_hf(uphf, phf)\
	pack_decoder_homing_frame(\
			uphf.LARc,\
			uphf.LTP_P1, uphf.LTP_b1, uphf.RPE_M1, uphf.RPE_xmaxc1,\
			uphf.RPE_xMc1,\
			uphf.LTP_P2, uphf.LTP_b2, uphf.RPE_M2, uphf.RPE_xmaxc2,\
			uphf.RPE_xMc2,\
			uphf.LTP_P3, uphf.LTP_b3, uphf.RPE_M3, uphf.RPE_xmaxc3,\
			uphf.RPE_xMc3,\
			uphf.LTP_P4, uphf.LTP_b4, uphf.RPE_M4, uphf.RPE_xmaxc4,\
			uphf.RPE_xMc4,\
			phf\
	);

#define rpe_ltp_encode(pcmf,uphf)\
		rpeltp_encoder(                  \
				&pcmf[0],        \
				uphf.LARc,           \
				uphf.LTP_P1,         \
				uphf.LTP_b1,         \
				uphf.RPE_M1,         \
				uphf.RPE_xmaxc1,     \
				uphf.RPE_xMc1,       \
				uphf.LTP_P2,         \
				uphf.LTP_b2,         \
				uphf.RPE_M2,         \
				uphf.RPE_xmaxc2,     \
				uphf.RPE_xMc2,       \
				uphf.LTP_P3,         \
				uphf.LTP_b3,         \
				uphf.RPE_M3,         \
				uphf.RPE_xmaxc3,     \
				uphf.RPE_xMc3,       \
				uphf.LTP_P4,         \
				uphf.LTP_b4,         \
				uphf.RPE_M4,         \
				uphf.RPE_xmaxc4,     \
				uphf.RPE_xMc4       \
		)


#define rpe_ltp_decode(uphf,pcmf)\
	rpeltp_decoder(                  \
		uphf.LARc,           \
		uphf.LTP_P1,         \
		uphf.LTP_b1,         \
		uphf.RPE_M1,         \
		uphf.RPE_xmaxc1,     \
		uphf.RPE_xMc1,       \
		uphf.LTP_P2,         \
		uphf.LTP_b2,         \
		uphf.RPE_M2,         \
		uphf.RPE_xmaxc2,     \
		uphf.RPE_xMc2,       \
		uphf.LTP_P3,         \
		uphf.LTP_b3,         \
		uphf.RPE_M3,         \
		uphf.RPE_xmaxc3,     \
		uphf.RPE_xMc3,       \
		uphf.LTP_P4,         \
		uphf.LTP_b4,         \
		uphf.RPE_M4,         \
		uphf.RPE_xmaxc4,     \
		uphf.RPE_xMc4,       \
		&pcmf[0]        \
	)

#define create_unpacked_hf(uhf) \
	uhf.LARc        = (int16_t*)malloc(8*sizeof(int16_t));\
	uhf.LTP_P1      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.LTP_b1      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_M1      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_xmaxc1  = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_xMc1    = (int16_t*)malloc(13*sizeof(int16_t));\
	uhf.LTP_P2      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.LTP_b2      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_M2      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_xmaxc2  = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_xMc2    = (int16_t*)malloc(13*sizeof(int16_t));\
	uhf.LTP_P3      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.LTP_b3      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_M3      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_xmaxc3  = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_xMc3    = (int16_t*)malloc(13*sizeof(int16_t));\
	uhf.LTP_P4      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.LTP_b4      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_M4      = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_xmaxc4  = (int16_t*)malloc(sizeof(int16_t));\
	uhf.RPE_xMc4    = (int16_t*)malloc(13*sizeof(int16_t))

#define destroy_unpacked_hf(uhf)\
		free(uhf.LARc      );\
		free(uhf.LTP_P1    );\
		free(uhf.LTP_b1    );\
		free(uhf.RPE_M1    );\
		free(uhf.RPE_xmaxc1);\
		free(uhf.RPE_xMc1  );\
		free(uhf.LTP_P2    );\
		free(uhf.LTP_b2    );\
		free(uhf.RPE_M2    );\
		free(uhf.RPE_xmaxc2);\
		free(uhf.RPE_xMc2  );\
		free(uhf.LTP_P3    );\
		free(uhf.LTP_b3    );\
		free(uhf.RPE_M3    );\
		free(uhf.RPE_xmaxc3);\
		free(uhf.RPE_xMc3  );\
		free(uhf.LTP_P4    );\
		free(uhf.LTP_b4    );\
		free(uhf.RPE_M4    );\
		free(uhf.RPE_xmaxc4);\
		free(uhf.RPE_xMc4  )

#endif /* MISC_H_ */
